<template>
	<!-- 刷题记录 -->
	<view class="content base-con-bg">
		<nav-bar leftText="刷题记录" leftType="3" bgColor="#fff"></nav-bar>
		<scroll-view scroll-x="true" class="scroll-y">
			<view class="scroll-list">
				<view class="item" :class="{'on': index===tabIndex}" v-for="(item,index) in ['每日提升','章节强化','错题攻克','万人模考']" :key="index" @click="tabClick(index)">{{item}}</view>
			</view>
		</scroll-view>
		<view v-if="tabIndex===0" class="list">
			<view @click="goDetail(item)" class="item" v-for="(item,index) in list1" :key="index">
				<view class="iconfont icon-dingdanjihe"></view>
				<view class="info">
					<view class="title">{{item.title}}</view>
					<view class="statistics">正确率{{item.rate}}%&emsp;答对:{{item.right}}道&emsp;答错:{{item.error}}道</view>
				</view>
				<view class="iconfont icon-fanhui base-fanhui"></view>
			</view>
		</view>
		<view v-if="tabIndex===1" class="list">
			<view @click="goDetail(item)" class="item" v-for="(item,index) in list2" :key="index">
				<view class="iconfont icon-dingdanjihe"></view>
				<view class="info">
					<view class="title">{{item.title}}</view>
					<view class="statistics">正确率{{item.rate}}%&emsp;答对:{{item.right}}道&emsp;答错:{{item.error}}道</view>
				</view>
				<view class="iconfont icon-fanhui base-fanhui"></view>
			</view>
		</view>
		<view v-if="tabIndex===2" class="list">
			<view @click="goDetail(item)" class="item" v-for="(item,index) in list3" :key="index">
				<view class="iconfont icon-dingdanjihe"></view>
				<view class="info">
					<view class="title">{{item.title}}</view>
					<view class="statistics">正确率{{item.rate}}%&emsp;答对:{{item.right}}道&emsp;答错:{{item.error}}道</view>
				</view>
				<view class="iconfont icon-fanhui base-fanhui"></view>
			</view>
		</view>
		<view v-if="tabIndex===3" class="list">
			<view @click="goDetail(item)" class="item" v-for="(item,index) in list4" :key="index">
				<view class="iconfont icon-dingdanjihe"></view>
				<view class="info">
					<view class="title">{{item.title}}</view>
					<view class="statistics">正确率{{item.rate}}%&emsp;答对:{{item.right}}道&emsp;答错:{{item.error}}道</view>
				</view>
				<view class="iconfont icon-fanhui base-fanhui"></view>
			</view>
		</view>
	</view>
</template>

<script>
	import {getHistory,getHistorySubjects} from "@/api/index.js"
	export default {
		data() {
			return {
				tabIndex: 0,
				page: 1,
				size: 5,
				list1: [],
				list2: [],
				list3: [],
				list4: [],
				loading: false,
				hasMore: true,
			}
		},
		computed: {
			getKeMuId() {
				return this.$store.getters.getKeMuId;
			},
			getKeMuParams() {
				return this.$store.getters.getKeMuParams;
			},
		},
		watch: {
			tabIndex(newVal){
				this['list'+(newVal+1)]=[];
				this.page=1;
				this.getHistory();
				this.loading= false;
				this.hasMore= true;
			}
		},
		onLoad() {
			this.getHistory()
		},
		onReachBottom() {
		    if (this.loading || !this.hasMore) return;
		    console.log('滚动到底部了');
		    this.getHistory();
		},
		methods: {
			getHistory(){
				let params={
					page: this.page,
					size: this.size,
					type: (this.tabIndex+1),//分类：1-每日提升，2-章节强化，3-错题攻克，4-万人模考
				};
				Object.assign(params,this.getKeMuParams);
				getHistory(params).then(res=>{
					const list=res?.data?.records;
					if (list.length > 0) {
						if(list.length==this.size){
							this.page++
						}else{
							this.hasMore = false;
						}
						this['list'+(this.tabIndex+1)]=this['list'+(this.tabIndex+1)].concat(list);
					} else {
						this.hasMore = false;
					}
				}).finally(()=>{
					this.loading = false;
				})
			},
			tabClick(index){
				this.tabIndex=index;
			},
			goDetail(item){
				getHistorySubjects({id: item.id}).then(res=>{
					this.$store.commit('subjectByDeptUpdata',{data: res.data.list})
					this.$store.commit('set',['isError',false]);
					uni.navigateTo({
						url: `/pagesC/topicRelated/answerSheet?type=1&aiAnalyze=${!!res.data.aiAnalyze ? '1' : ''}&isOldValue=1&id=${res.data?.aiAnalyze?.id}`
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		min-height: 110vh;
	}
	.scroll-y{
		margin-bottom: 36rpx;
	}
	.scroll-list{
		display: flex;
		padding: 28rpx 22rpx 0 48rpx;
		color: #333;
		.item{
			position: relative;
			margin-right: 38rpx;
			padding-bottom: 14rpx;
			white-space: nowrap;
			font-size: 28rpx;
			&:last-child{
				padding-right: 50rpx;
			}
			&.on{
				font-size: 30rpx;
				font-weight: 600;
				color: #222;
				&::after{
					content: "";
					position: absolute;
					bottom: 0;
					left: 50%;
					width: 44rpx;
					height: 6rpx;
					margin-left: -22rpx;
					background-color: #FF9813;
					border-radius: 2rpx;
				}
			}
		}
	}
	.list{
		padding: 0 28rpx;
		.item{
			display: flex;
			align-items: center;
			width: 694rpx;
			height: 126rpx;
			margin-bottom: 20rpx;
			padding: 0 35rpx 0 34rpx;
			border: 2rpx solid #EAEAEA;
			border-radius: 26rpx;
		}
		.iconfont{
			flex-shrink: 0;
			color: #747474;
			&:nth-child(1){
				font-size: 66rpx;
			}
			&:nth-child(3){
				font-size: 30rpx;
			}
		}
		.info{
			flex: 1;
			overflow: hidden;
			margin: 0 32rpx;
		}
		.title{
			margin-bottom: 6rpx;
			font-size: 26rpx;
			font-weight: 600;
			color: #323232;
		}
		.statistics{
			font-size: 24rpx;
			color: #9A9A9A;
		}
	}
</style>
